package main

import ll "linkedlist"

func rotateRight(head *ll.ListNode, k int) *ll.ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	//记录链表长度
	len := 1
	//找到链表尾结点
	prev := head
	for ; prev.Next != nil; prev = prev.Next {
		len++
	}
	//把链表连成环
	prev.Next = head
	k = k % len
	//找到断开的位置（移动len-k-1次）
	prev = head
	for i := 0; i < len-k-1; i++ {
		prev = prev.Next
	}
	newHead := prev.Next
	prev.Next = nil
	return newHead
}
